// 引入
const express = require('express');
const db = require('../../utils/db');

// 配置路由
const bianxieRouter = express.Router();

// 编写接口
bianxieRouter.get('/get',(req,res)=>{ // 获取数据
    const page = req.query.page;
    const count = req.query.count;
    const sql = `select * from t_zhaopin limit ${(page-1)*count},${count}`;
    const sql2 = `select count(*) as total from t_zhaopin;`;
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            db.query(sql2,(err2,data2)=>{
                if(err2){
                    console.log(err2)
                    res.send({error:1,msg:err2})
                }else{
                    console.log(data2)
                    res.send({error:0,msg:'查询成功',data:data,total:data2[0].total})
                }
                
            })
           
        }
    })
})
bianxieRouter.get('/chaxun',(req,res)=>{
    const page = req.query.page;
    const count = req.query.count;
    const sql = `SELECT * FROM t_zhaopin WHERE zhaopin_yx='${req.query.zhaopin_yx}'`
    const sql2 = `SELECT COUNT(*) as total FROM t_zhaopin WHERE zhaopin_yx = '${req.query.zhaopin_yx}'`
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            console.log(data)
            db.query(sql2,(err2,data2)=>{
                if(err2){
                    console.log(err2)
                    res.send({error:1,msg:err2})
                }else{
                    console.log(data2)
                    res.send({error:0,msg:'查询成功',data:data,total:data2[0].total})
                }
            })
        }
    })
   
})

//性别查询接口
bianxieRouter.get('/chaxun1',(req,res)=>{
    const page = req.query.page;
    const count = req.query.count;
    const sql = `SELECT * FROM t_zhaopin WHERE zhaopin_xb='${req.query.zhaopin_xb}'`
    const sql2 = `SELECT COUNT(*) as total FROM t_zhaopin WHERE zhaopin_xb = '${req.query.zhaopin_xb}'`
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            console.log(data)
            db.query(sql2,(err2,data2)=>{
                if(err2){
                    console.log(err2)
                    res.send({error:1,msg:err2})
                }else{
                    console.log(data2)
                    res.send({error:0,msg:'查询成功',data:data,total:data2[0].total})
                }
            })
        }
    })
   
})


//需求查询接口
bianxieRouter.get('/chaxun2',(req,res)=>{
    const page = req.query.page;
    const count = req.query.count;
    const sql = `SELECT * FROM t_zhaopin WHERE zhaopin_xq='${req.query.zhaopin_xq}'`
    const sql2 = `SELECT COUNT(*) as total FROM t_zhaopin WHERE zhaopin_xq = '${req.query.zhaopin_xq}'`
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            console.log(data)
            db.query(sql2,(err2,data2)=>{
                if(err2){
                    console.log(err2)
                    res.send({error:1,msg:err2})
                }else{
                    console.log(data2)
                    res.send({error:0,msg:'查询成功',data:data,total:data2[0].total})
                }
            })
        }
    })
   
})
//input查询
bianxieRouter.get('/input',(req,res)=>{
    const sql = `SELECT * FROM t_zhaopin WHERE zhaopin_zw='${req.query.zhaopin_zw}'`
    const sql2 = `SELECT COUNT(*) as total FROM t_zhaopin WHERE zhaopin_zw = '${req.query.zhaopin_zw}'`
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            console.log(data)
            db.query(sql2,(err2,data2)=>{
                if(err2){
                    console.log(err2)
                    res.send({error:1,msg:err2})
                }else{
                    console.log(data2)
                    res.send({error:0,msg:'查询成功',data:data,total:data2[0].total})
                }
            })
        }
    })
})
//删除
bianxieRouter.post('/del',(req,res)=>{
    const sql = `delete from t_zhaopin where zhaopin_id='${req.body.zhaopin_id}';`
    db.query(sql,(err,data)=>{
if(err){
    console.log(err)
    res.send({error:1,msg:err})
}else{
    res.send({error:0,msg:'删除成功'})
}
    })
})
//添加
bianxieRouter.post('/add',(req,res)=>{
    const sql = `INSERT INTO t_zhaopin(zhaopin_id,zhaopin_zw,zhaopin_rs,zhaopin_yx,zhaopin_yy,zhaopin_yuex,zhaopin_xb,zhaopin_sqrq,zhaopin_dgrq,zhaopin_xq,zhaopin_cjr,zhaopin_cjsj,zhaopin_gxr)
    VALUES(null,'${req.body.zhaopin_zw}','${req.body.zhaopin_rs}','${req.body.zhaopin_yx}','${req.body.zhaopin_yy}','${req.body.zhaopin_yuex}','${req.body.zhaopin_xb}','${req.body.zhaopin_sqrq}','${req.body.zhaopin_dgrq}', '${req.body.zhaopin_xq}','${req.body.zhaopin_cjr}','${req.body.zhaopin_cjsj}','${req.body.zhaopin_gxr}');`
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            res.send({error:0,msg:'添加成功'})
        }
    })
})
//修改
bianxieRouter.post('/edit',(req,res)=>{
    const sql = `UPDATE t_zhaopin SET zhaopin_zw ='${req.body.zhaopin_zw}' ,zhaopin_rs = '${req.body.zhaopin_rs}',zhaopin_yx = '${req.body.zhaopin_yx}',zhaopin_yy = '${req.body.zhaopin_yy}',zhaopin_yuex = '${req.body.zhaopin_yuex}',zhaopin_xb = '${req.body.zhaopin_xb}',zhaopin_sqrq = '${req.body.zhaopin_sqrq}',zhaopin_dgrq ='${req.body.zhaopin_dgrq}' ,zhaopin_xq = '${req.body.zhaopin_xq}',zhaopin_cjr = '${req.body.zhaopin_cjr}',zhaopin_cjsj = '${req.body.zhaopin_cjsj}',zhaopin_gxr ='${req.body.zhaopin_gxr}'WHERE zhaopin_id ='${req.body.zhaopin_id}';`;
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            res.send({error:0,msg:'修改成功'})
        }
    })
})


// 导出
module.exports = bianxieRouter;